package com.ys.timer;

import com.alibaba.fastjson.JSON;
import com.ys.entity.purchase.TcgPurchaseByOrder;
import com.ys.enume.purchase.TcgPurchaseByOrderEnum;
import com.ys.service.log.ApiLogService;
import com.ys.service.purchase.Api1688Service;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * 1688定时器
 */
@Controller
public class Timer1688OrderDaily {

	private static final Logger logger = LoggerFactory.getLogger(Timer1688OrderDaily.class);


	@Autowired
	private Api1688Service api1688Service;

	@Autowired
	private ApiLogService apiLogService;
	
	/*
	 * 每2小时获取1688订单物流信息
	 */
	@Scheduled(cron = "0 0 0/2 * * * ")
	public void get1688OrderLogisticsInfo() {
		logger.error("【1688接口】定时获取1688订单物流信息开始");
		try {
			List<TcgPurchaseByOrder> tcgPurchaseOrderList = new TcgPurchaseByOrder()
					.setOrderStatus(TcgPurchaseByOrderEnum.OrderStatus.PAY_FINISH.value)
					.where("A.AliLogisticsBillNo IS NULL AND A.aliOrderNumber IS NOT NULL AND A.aliState = 'waitsellersend'")
					.queryList();
			for (TcgPurchaseByOrder tcgPurchaseOrder : tcgPurchaseOrderList){
				R r = api1688Service.get1688OrderLogisticsInfo(tcgPurchaseOrder.getBid());
				if (r.getCode() == R.Code.FAILED.value) {
					logger.error("【1688接口】定时获取1688订单物流信息出错，错误信息为：" + r.getMsg());
					apiLogService.addApiLog("1688", "get1688OrderLogisticsInfo", "Bid：" + JSON.toJSONString(tcgPurchaseOrder.getBid()), "ECP_API调用获取1688订单物流信息接口出错，错误为："+r.getMsg(), r.getCode(), r.getMsg());
				}
			}
			logger.error("【1688接口】定时获取1688订单物流信息结束");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
